/**
 * Created by lxh on 2018/1/5.
 */
import {queryAllSeller,updateSeller,queryShopGoodById,insertShopGoods,deleteShopGoods,queryAllGoods} from '../services/adminManageApi';

export default{
    namespace: 'shopManage',
    state: {
        submitting:false,
        data:{
            list:[],
            pagination:{},
        },
        GoodsData:{
            list:[],
            pagination:{},
        },
        allGoodsData:{
            list:[],
            pagination:{},
        }
    },
    effects: {
        *queryAll({payload, callback}, {put, call}){
            yield put({
                type: 'changeLoading',
                payload: true,
            })
            const response = yield call(queryAllSeller, payload);
            yield put({
                type: 'handleResponse',
                payload: response,
            })
            yield put({
                type: 'changeLoading',
                payload: false,
            })
        },
        *updateShop({payload, callback}, {put, call}){
            const response = yield call(updateSeller, payload);
            if (callback) callback(response);
        },
        *queryShopGoodById({payload,callback},{put,call}){
            const response=yield call(queryShopGoodById,payload)
            yield put({
                type:'handleGoods',
                payload:response,
            })
        },
        *insertShopGoods({payload,callback},{put,call}){
            const response=yield call(insertShopGoods,payload)
            if(callback)callback(response);
        },
        *deleteShopGoodsById({payload,callback},{put,call}){
            const response=yield call(deleteShopGoods,payload)
            if(callback)callback(response);
        },
        *queryAllGoods({payload,callback},{put,call}){
            const response=yield call(queryAllGoods,payload)
            yield put({
                type:'handleAllGoods',
                payload:response,
            })
        },
    },
    reducers: {
        changeLoading(state,{payload}){
            return ({
                ...state,
               submitting:payload,
            })
        },
        handleResponse(state,{payload}){
            return ({
                ...state,
                data:payload.resultData.allSeller,
            })
        },
        handleGoods(state,{payload}){
            return({
                ...state,
                GoodsData:payload.resultData.ShopGoods,
            })
        },
        handleAllGoods(state,{payload}){
            return ({
                ...state,
                allGoodsData:payload.resultData.allGoods,
            })
        }
    },
}
